package com.supergzt.utils;



import com.supergzt.constant.StringConstants;

import java.util.Collection;

/**
 * @author 张钧
 * @Description
 * @create 2022-10-20 17:12
 */
public class StringUtils {
	public static boolean isAnyBlank(String... strs) {
		for (String str : strs) {
			if (isBlank(str)) {
				return true;
			}
		}
		return false;
	}

	public static <T> boolean isBlank(T str) {
		return isNull(str);
	}

	public static <T> boolean isNull(T str) {
		return str == null || str.equals(StringConstants.NULL);
	}

	public static <T> boolean isNotNull(T str) {
		return !isNull(str);
	}

	public static <T> T defaultIfEmpty(T str, T defaultValue) {
		return isNull(str) ? defaultValue : str;
	}

	public static boolean contains(String str, String c) {
		return str.contains(c);
	}

	public static <T> boolean containAny(Collection<T> str, T key) {
		for (T element : str) {
			if (element.equals(key)) {
				return true;
			}
		}
		return false;
	}

	public static <T> boolean containsAny(Collection<T> str, Collection<T> keys) {
		if (isNull(str) || isNull(keys)) {
			return false;
		}
		for (T key : keys) {
			if (str.contains(key)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isNumeric(String str) {
		for (int i = str.length(); --i >= 0; ) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 字符串是否在给定区间
	 *
	 * @param str         字符串
	 * @param max         最大
	 * @param includedMax 是否包含最大
	 * @param min         最小
	 * @param includedMin 是否包含最小
	 */
	public static boolean lengthBetween(String str, Integer max, boolean includedMax, Integer min, boolean includedMin) {
		return includedMax ? lengthLe(str, max) : lengthLt(str, max) && includedMin ? lengthGe(str, min) : lengthGt(str, min);
	}

	/**
	 * 字符串长度小于给定长度
	 *
	 * @param str     字符串
	 * @param integer 给定长度
	 */
	public static boolean lengthLt(String str, Integer integer) {
		return str.length() < integer;
	}

	/**
	 * 字符串长度小于等于给定长度
	 *
	 * @param str     字符串
	 * @param integer 给定长度
	 */
	public static boolean lengthLe(String str, Integer integer) {
		return str.length() <= integer;
	}

	/**
	 * 字符串长度大于给定长度
	 *
	 * @param str     字符串
	 * @param integer 给定长度
	 */
	public static boolean lengthGt(String str, Integer integer) {
		return str.length() > integer;
	}

	/**
	 * 字符串长度大于等于给定长度
	 *
	 * @param str     字符串
	 * @param integer 给定长度
	 */
	public static boolean lengthGe(String str, Integer integer) {
		return str.length() >= integer;
	}

	/**
	 * 字符串长度等于给定长度
	 *
	 * @param str     字符串
	 * @param integer 给定长度
	 */
	public static boolean lengthEqual(String str, Integer integer) {
		return str.length() == integer;
	}

	public static String format(String str, String... strs) {
		StringBuilder stringBuffer = new StringBuilder(str);
		for (String s : strs) {
			int start = stringBuffer.indexOf("{");
			int end = stringBuffer.indexOf("}");
			if(start == end){
				break;
			}
			stringBuffer.replace(start,  end+ 1, s);
		}
		return stringBuffer.toString();
	}

	public static String substring(String str, int start, int end) {
		if (str == null) {
			return StringConstants.NULL;
		}
		if (end < 0) {
			end = str.length() + end;
		}
		if (start < 0) {
			start = str.length() + start;
		}
		if (end > str.length()) {
			end = str.length();
		}
		if (start > end) {
			return StringConstants.NULL;
		}
		if (start < 0) {
			start = 0;
		}
		if (end < 0) {
			end = 0;
		}
		return str.substring(start, end);
	}
}
